package com.bicon.botu.medical.service.impl

import com.baomidou.mybatisplus.mapper.EntityWrapper
import com.baomidou.mybatisplus.service.impl.ServiceImpl
import com.bicon.botu.core.base.common.BusinessException
import com.bicon.botu.core.base.common.Constants
import com.bicon.botu.core.base.common.utils.BeanUtils
import com.bicon.botu.core.base.common.utils.StringUtils
import com.bicon.botu.medical.common.constant.FoodConstants
import com.bicon.botu.medical.dao.mapper.IndexRecipesMapper
import com.bicon.botu.medical.model.dto.FoodComponentDTO
import com.bicon.botu.medical.model.dto.FoodDTO
import com.bicon.botu.medical.model.entity.Dictionary
import com.bicon.botu.medical.model.entity.Food
import com.bicon.botu.medical.model.entity.FoodComponent
import com.bicon.botu.medical.model.entity.IndexRecipes
import com.bicon.botu.medical.service.IDictionaryService
import com.bicon.botu.medical.service.IIndexRecipesService
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import kotlin.collections.HashMap


/**
 * 食谱关系Service实现类
 * @author 李双喜
 * @date 2018/7/18 17:28
 */
@Slf4j
@Service
open class IndexRecipesServiceImpl : ServiceImpl<IndexRecipesMapper, IndexRecipes>(), IIndexRecipesService {

    @Autowired
    open lateinit var iDictionaryService: IDictionaryService

    /**
     * 食物详情
     *
     * @param foodId 食物id
     * @return
     */
    override fun getFoodDetails(foodId: Int): FoodDTO {
        val food = Food().selectById(foodId)
        if (StringUtils.isEmpty(food)) {
            throw BusinessException("食物不存在")
        }
        var foodDTO: FoodDTO? = null
        foodDTO = FoodDTO()
        BeanUtils.copy(food, foodDTO)
        //查找食物成分
        val foodComponentList = FoodComponent().selectList(EntityWrapper<FoodComponent>().eq("food_id", food.id).eq("logic_delete", Constants.DEL_LOGIC_NORMAL))
        val weightList: MutableList<FoodComponentDTO> = ArrayList<FoodComponentDTO>()
        val nutritionList: MutableList<FoodComponentDTO> = mutableListOf()
        val foodList: MutableList<FoodComponentDTO> = mutableListOf()
        for (foodComponent in foodComponentList) {
            var foodComponentDTO: FoodComponentDTO? = null
            foodComponentDTO = FoodComponentDTO()
            if (foodComponent.name == "热量" || foodComponent.name == "蛋白质" || foodComponent.name == "脂肪" || foodComponent.name == "碳水化合物") {
                if (-1.0 == foodComponent.amount) {
                    foodComponent.amount = 0.0
                }
                setFoodComponentDTO(foodComponentDTO, foodComponent)
                foodList.add(foodComponentDTO)
            }
            //营养元素
            if (FoodConstants.COMPONENT_TYPE_NUTRITION.equals(foodComponent.type)) {
                setFoodComponentDTO(foodComponentDTO, foodComponent)
                nutritionList.add(foodComponentDTO)
                if (-1.0 == foodComponent.amount) {
                    nutritionList.remove(foodComponentDTO)
                }
            }
        }
        val map: MutableMap<String, List<FoodComponentDTO>> = mutableMapOf()
        val dictionaryList = iDictionaryService.selectList(EntityWrapper<Dictionary>().eq("parent_id", food.weightStandardId).eq("logic_delete", Constants.DEL_LOGIC_NORMAL))
        for (dictionary in dictionaryList) {
            val foodComponentDTO = FoodComponentDTO()
            foodComponentDTO.name = dictionary.dictionaryName
            val content = dictionary.content!!.split("/".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
            foodComponentDTO.weight = FoodConstants.FOOD_WEIGHT.toString()
            foodComponentDTO.amount = java.lang.Double.parseDouble(content[0])
            foodComponentDTO.unit = content[1]
            weightList.add(foodComponentDTO)
        }
        map[FoodConstants.COMPONENT_TYPE_WEIGHT_STRING] = weightList
        map[FoodConstants.COMPONENT_TYPE_NUTRITION_STRING] = nutritionList
        map["FIXED_COMPOSITION"] = foodList
        foodDTO.listMap = map
        return foodDTO
    }


    /**
     * 设置食物成分
     *
     * @param foodComponentDTO 食物成分DTO
     * @param foodComponent    食物成分
     */
    private fun setFoodComponentDTO(foodComponentDTO: FoodComponentDTO, foodComponent: FoodComponent) {
        foodComponentDTO.name = foodComponent.name
        foodComponentDTO.weight = foodComponent.weight
        foodComponentDTO.amount = foodComponent.amount
        foodComponentDTO.unit = foodComponent.unit
    }


}